﻿@model Invoicing.Model.Products.ProductRatioModel

<div class="GeneralEdit" id="fmRatio">
    <ul>
        <li>
            <label>产品：</label>
            <input id="selProduct" data-value="@Model.ProductID" />
        </li>
        <li>
            <label>材料：</label>
            <select id="selMaterial"></select>
        </li>
        <li>
            <label>规格：</label>
            <select id="selSize"></select>
        </li>
        <li>
            <label for="txtRatio">系数：</label>
            <input type="text" id="txtRatio" class="k-textbox" name="ratio" required validationmessage="请输入系数" value="@Model.Ratio" />
            <span class="k-invalid-msg" data-for="ratio"></span>
        </li>
        <li class="editButtons">
            <button id="btnSaveRatio" class="k-button"><span class="k-icon k-i-tick"></span><span>确认提交</span></button>
            &nbsp;&nbsp;
            <button class="k-button" onclick="CancelGeneralEdit();"><span class="k-icon k-i-cancel"></span><span>&nbsp;返&nbsp;回</span></button>
        </li>
    </ul>
</div>

<script class="init">

    $("#selProduct").kendoComboBox({
        placeholder: "请选择产品...",
        filter: "contains",
        dataTextField: "Module",
        dataValueField: "ID",
        dataSource: products,
        autoBind: false,
        text:"@Model.Module",
        select: function (e) {
            var item = this.dataItem(e.item.index());
            $("#selProduct").attr("data-value", item.ID);
        },
        change: function (e) {
            if (!$.isNumeric(this.value())) {
                this.text("");
                $("#selProduct").attr("data-value", 0);
            }
        }
    });

    $("#selMaterial").kendoDropDownList({
        dataTextField: "MaterialName",
        dataValueField: "ID",
        dataSource: materials,
        dataBound: function (e) {
            if ("@Model.MaterialID" > 0) {
                this.select(function (dataItem) {
                    return dataItem.ID == "@Model.MaterialID";
                });
            }
        }
    });

    $("#selSize").kendoDropDownList({
            dataTextField: "SizeName",
            dataValueField: "ID",
            dataSource: sizes,
            dataBound: function (e) {
                if ("@Model.Size" !="") {
                this.select(function (dataItem) {
                    return dataItem.SizeName == "@Model.Size";
                });
            }
        }
    });
</script>

<script class="save_ratio">
    $(function () {
        var validator = $("#fmRatio").kendoValidator().data("kendoValidator");
        $("#btnSaveRatio").click(function (e) {
            if (validator.validate()) {
                var values = {
                    ratioID: "@Model.ID",
                    productID: $("#selProduct").attr("data-value"),
                    materialID: $("#selMaterial").data("kendoDropDownList").value(),
                    size: $("#selSize").data("kendoDropDownList").text(),
                    ratio: $("#txtRatio").val()
                };

                Whale.Remote.Operation({
                    url: "/Products/Ratio/Save",
                    parameters: values,
                    success: function (result) {
                        wndGeneralEdit.close();
                        if ($("#grid_ratios").length > 0)
                            $("#grid_ratios").data("kendoGrid").dataSource.read();
                    },
                    error: function (msg) {
                        wndGeneralInformation.content(msg).open();
                    }
                });
            }
            return false;
        });
    });
</script>
